package com.tangkun.environment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.entity.personcenter.NoticeReturn;
import com.tangkun.environment.utils.LinkifySpannableUtils;
import com.tangkun.environment.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: XiTongTongZhiAdapter
 * @Description: 系统通知列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class XiTongTongZhiAdapter extends BaseAdapter {

    private Context context;
    private List<NoticeReturn.DataBean.ListBean> dataList = new ArrayList<NoticeReturn.DataBean.ListBean>();

    public XiTongTongZhiAdapter(Context context, List<NoticeReturn.DataBean.ListBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_xitongtongzhi, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            //标题
            if (!TextUtils.isEmpty(dataList.get(position).title)) {
                holder.tvXitongtongzhiTongzhibiaoti.setText(dataList.get(position).title);
            } else {
                holder.tvXitongtongzhiTongzhibiaoti.setText("");
            }
            //时间
            if (!TextUtils.isEmpty(dataList.get(position).createtime)) {
                holder.tvXitongtongzhiShijian.setText(Utils.getDateMonthDayHourMinute(dataList.get(position).createtime));
            } else {
                holder.tvXitongtongzhiShijian.setText("");
            }
            //内容
            if (!TextUtils.isEmpty(dataList.get(position).content)) {
                holder.tvXitongtongzhiNeirong.setText(dataList.get(position).content);
                LinkifySpannableUtils.getInstance().setSpan(context, holder.tvXitongtongzhiNeirong);
            } else {
                holder.tvXitongtongzhiNeirong.setText("");
            }

//            //查看详情
//            convertView.setOnClickListener(new View.OnClickListener() {
//
//                @Override
//                public void onClick(View v) {
//                    Intent i = new Intent(v.getContext(), WebActivity.class);
//                    i.putExtra("url", "");
//                    i.putExtra("title", "通知详情");
//                    v.getContext().startActivity(i);
//                }
//            });
        }
        return convertView;

    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.tv_xitongtongzhi_tongzhibiaoti)
        TextView tvXitongtongzhiTongzhibiaoti;
        @BindView(R.id.tv_xitongtongzhi_shijian)
        TextView tvXitongtongzhiShijian;
        @BindView(R.id.tv_xitongtongzhi_neirong)
        TextView tvXitongtongzhiNeirong;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
